package com.sims.daoImp;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.sims.bean.Student;
import com.sims.dao.IStudentDAO;

/**
 * A data access object (DAO) providing persistence and search support for
 * Student entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.sims.bean.Student
 * @author MyEclipse Persistence Tools
 */

public class StudentDAO extends HibernateDaoSupport implements IStudentDAO {
	private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);
	// property constants
	public static final String SNO = "sno";
	public static final String PASSWORD = "password";
	public static final String NAME = "name";
	public static final String SEX = "sex";
	public static final String NATION = "nation";
	public static final String DEGREE = "degree";
	public static final String POLITICAL_STATUS = "politicalStatus";
	public static final String IDCARD = "idcard";
	public static final String PHOTO_LOCATION = "photoLocation";
	public static final String CID = "cid";
	public static final String SEAT_NO = "seatNo";
	public static final String TEL = "tel";
	public static final String PROFILE = "profile";

	@Override
	protected void initDao() {
		// do nothing
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#save(com.sims.bean.Student)
	 */
	@Override
	public void save(Student transientInstance) {
		log.debug("saving Student instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#delete(com.sims.bean.Student)
	 */
	@Override
	public void delete(Student persistentInstance) {
		log.debug("deleting Student instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findById(java.lang.Integer)
	 */
	@Override
	public Student findById(java.lang.Integer id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Student instance = (Student) getHibernateTemplate().get(
					"com.sims.bean.Student", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByExample(com.sims.bean.Student)
	 */
	@Override
	public List findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByProperty(java.lang.String, java.lang.Object)
	 */
	@Override
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Student instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Student as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findBySno(java.lang.Object)
	 */
	@Override
	public List findBySno(Object sno) {
		return findByProperty(SNO, sno);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByPassword(java.lang.Object)
	 */
	@Override
	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByName(java.lang.Object)
	 */
	@Override
	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findBySex(java.lang.Object)
	 */
	@Override
	public List findBySex(Object sex) {
		return findByProperty(SEX, sex);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByNation(java.lang.Object)
	 */
	@Override
	public List findByNation(Object nation) {
		return findByProperty(NATION, nation);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByDegree(java.lang.Object)
	 */
	@Override
	public List findByDegree(Object degree) {
		return findByProperty(DEGREE, degree);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByPoliticalStatus(java.lang.Object)
	 */
	@Override
	public List findByPoliticalStatus(Object politicalStatus) {
		return findByProperty(POLITICAL_STATUS, politicalStatus);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByIdcard(java.lang.Object)
	 */
	@Override
	public List findByIdcard(Object idcard) {
		return findByProperty(IDCARD, idcard);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByPhotoLocation(java.lang.Object)
	 */
	@Override
	public List findByPhotoLocation(Object photoLocation) {
		return findByProperty(PHOTO_LOCATION, photoLocation);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByCid(java.lang.Object)
	 */
	@Override
	public List findByCid(Object cid) {
		return findByProperty(CID, cid);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findBySeatNo(java.lang.Object)
	 */
	@Override
	public List findBySeatNo(Object seatNo) {
		return findByProperty(SEAT_NO, seatNo);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByTel(java.lang.Object)
	 */
	@Override
	public List findByTel(Object tel) {
		return findByProperty(TEL, tel);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findByProfile(java.lang.Object)
	 */
	@Override
	public List findByProfile(Object profile) {
		return findByProperty(PROFILE, profile);
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#findAll()
	 */
	@Override
	public List findAll() {
		log.debug("finding all Student instances");
		try {
			String queryString = "from Student";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#merge(com.sims.bean.Student)
	 */
	@Override
	public Student merge(Student detachedInstance) {
		log.debug("merging Student instance");
		try {
			Student result = getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#attachDirty(com.sims.bean.Student)
	 */
	@Override
	public void attachDirty(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/* (non-Javadoc)
	 * @see com.sims.daoImp.IStudentDAO#attachClean(com.sims.bean.Student)
	 */
	@Override
	public void attachClean(Student instance) {
		log.debug("attaching clean Student instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static IStudentDAO getFromApplicationContext(ApplicationContext ctx) {
		return (IStudentDAO) ctx.getBean("StudentDAO");
	}

	@Override
	public Student findBySno(String sno) {
		String hql = "from Student as s where s.sno = :value";
		Query query = getSession().createQuery(hql);
		query.setParameter("value", sno);
		return (Student)query.uniqueResult();
	}
}